home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / IFC_112 / netscape / application / KeyStroke.java < prev    next >
Encoding:
Text File  |  1999-05-28  |  3.3 KB  |  129 lines  |  [TEXT/CWIE]

  1. // KeyStroke.java
  2. // By Ned Etcode
  3. // Copyright 1996, 1997 Netscape Communications Corp.  All rights reserved.
  4.  
  5. package netscape.application;
  6.  
  7. import netscape.util.*;
  8.  
  9. import java.awt.Event;
  10.  
  11. /** A class to represent key strokes.
  12.  * @private
  13.  */
  14. public class KeyStroke implements Codable {
  15.     int key;
  16.     int modifiers;
  17.  
  18.     final static String KEY_KEY = "key";
  19.     final static String MODIFIERS_KEY = "modifiers";
  20.  
  21.  
  22.  
  23.     /** Default constructor for archiving **/
  24.     public KeyStroke() {
  25.         super();
  26.     }
  27.  
  28.     /** Create a new key stroke that represents a <b>key</b> pressed
  29.      *  with the <b>modifiers</b> down.
  30.      */
  31.     public KeyStroke(int key,int modifiers) {
  32.         this.key = key;
  33.         this.modifiers = modifiers;
  34.     }
  35.  
  36.     /** Create a new key stroke that matches <b>anEvent</b> **/
  37.     public KeyStroke(KeyEvent anEvent) {
  38.         this(anEvent.key,anEvent.modifiers);
  39.     }
  40.  
  41.     /** Return the key for this key stroke **/
  42.     public int key() {
  43.         return key;
  44.     }
  45.  
  46.     /** Return the modifiers for this key stroke **/
  47.     public int modifiers() {
  48.         return modifiers;
  49.     }
  50.  
  51.     public boolean equals(Object anotherKey) {
  52.         if(!(anotherKey instanceof KeyStroke))
  53.             return false;
  54.  
  55.         if(key == ((KeyStroke)anotherKey).key &&
  56.            modifiers == ((KeyStroke)anotherKey).modifiers)
  57.             return true;
  58.         else {
  59.             /** BIG hack to workaround keycode changing when control is down **/
  60.             if((modifiers & KeyEvent.CONTROL_MASK) > 0) {
  61.                 int ak = (((KeyStroke)anotherKey).key() + 64);
  62.                 int mk = key;
  63.                 if(ak >= 'a' && ak <= 'z')
  64.                     ak -= ('a' - 'A');
  65.                 if(mk >= 'a' && mk <= 'z')
  66.                     mk -= ('a' - 'A');
  67.                 if(ak == mk)
  68.                     return true;
  69.                 else
  70.                     return false;
  71.             }
  72.             else
  73.                 return false;
  74.         }
  75.     }
  76.  
  77.     public boolean matchesKeyEvent(KeyEvent anEvent) {
  78.         if( anEvent == null )
  79.             return false;
  80.         if( anEvent.type == KeyEvent.KEY_DOWN &&
  81.             anEvent.key == key &&
  82.             anEvent.modifiers == modifiers )
  83.             return true;
  84.         else
  85.             return false;
  86.     }
  87.  
  88.    public int hashCode() {
  89.         return ("" + this).hashCode();
  90.    }
  91.  
  92.    public String toString() {
  93.        return "KeyStroke ("+key+","+modifiers+")";
  94.    }
  95.  
  96.     /* archiving */
  97.  
  98.     /** Describes the KeyStroke's class information.
  99.       * @see Codable#describeClassInfo
  100.       */
  101.     public void describeClassInfo(ClassInfo info) {
  102.         info.addClass("netscape.application.KeyStroke", 1);
  103.         info.addField(KEY_KEY, INT_TYPE);
  104.         info.addField(MODIFIERS_KEY,INT_TYPE);
  105.     }
  106.  
  107.     /** Encodes the View instance.
  108.       * @see Codable#decode
  109.       */
  110.     public void encode(Encoder encoder) throws CodingException {
  111.         encoder.encodeInt(KEY_KEY,key);
  112.         encoder.encodeInt(MODIFIERS_KEY,modifiers);
  113.     }
  114.  
  115.     /** Decodes the View instance.
  116.       * @see Codable#decode
  117.       */
  118.     public void decode(Decoder decoder) throws CodingException {
  119.         key = decoder.decodeInt(KEY_KEY);
  120.         modifiers = decoder.decodeInt(MODIFIERS_KEY);
  121.     }
  122.  
  123.     /** Finishes the View instance decoding.
  124.       * @see Codable#finishDecoding
  125.       */
  126.     public void finishDecoding() throws CodingException {
  127.     }
  128. }
  129.